//
//  Section1Scene.swift
//  Space Experiment Module
//
//  Created by 曹亚索 on 2020/12/18.
//
//MARK:Section 2

import SpriteKit
import AudioToolbox

class Section2Scene: SKScene ,SKPhysicsContactDelegate{
    
    var viewcontroller : UIViewController?
    var contentCreated = false
    var background1 : SKSpriteNode!
    var background2 : SKSpriteNode!
    var deltaTime : TimeInterval = 0
    var lastFrameTime : TimeInterval = 0
    var plantSprite : SKSpriteNode!
    var insect : SKSpriteNode!
    var sun : SKSpriteNode!
    var plantposition : Int = 1
    var gametime : TimeInterval!
    var score : Int = 0
    var scoreLabel : SKLabelNode!
    var speedLabel : SKLabelNode!
    var back : UIButton!
    var gamespeed : Double = 1
    var sudu : Double! = 1
    var restart : Bool!
    var health : Int!
    var alert : UIAlertController!
    var answerResult : String!
    //阶段加速
    var speed1 = false
    var speed2 = false
    var speed3 = false
    var speed4 = false
    
    
    //MARK:didMove
    override func didMove(to view: SKView) {
        self.physicsWorld.gravity = CGVector(dx: 0, dy: 0)
        self.physicsWorld.contactDelegate = self
        if self.contentCreated == false{
            self.createSceneContents()
            contentCreated = true
            //加载背景
            setupBackGround()
            //加载返回按钮
            setupBackBtn()
            //加载精灵
            setupCharacters()
            //set分数
            setupLabel()
            currentScore = 0
            //set血量
            health = characterArray[currentCharacter].Level
            //加载血条
            setupHealth(sender: self, health: health)
        }
    }
    
    func createSceneContents(){
        self.backgroundColor = SKColor.black
        self.contentCreated = true
    }
    
    func setupBackGround(){
        background1 = SKSpriteNode(texture: SKTexture(imageNamed: "gameScene2"),size: CGSize(width: size.width, height: size.height))
        background1.position = CGPoint(x: size.width / 2, y: size.height / 2)
        background2 = (background1.copy() as! SKSpriteNode)
        background2.position = CGPoint(x: background1.position.x + size.width, y: background1.position.y)
        self.addChild(background1)
        self.addChild(background2)
    }
    
    //MARK:scoreLabel
    func setupLabel(){
        scoreLabel = SKLabelNode(fontNamed:"MarkerFelt-Wide")
        scoreLabel.fontColor = UIColor.black
        scoreLabel.position = CGPoint(x: self.frame.midX, y: 3 * self.frame.size.height / 4)
        scoreLabel.zPosition = 100
        scoreLabel.text = "\(score)"
        self.addChild(scoreLabel)
    }
    
    //MARK:speed rise label
    func speedUpLabel(){
        speedLabel = SKLabelNode(fontNamed:"MarkerFelt-Wide")
        speedLabel.fontColor = UIColor.red
        speedLabel.position = CGPoint(x: UIScreen.main.bounds.maxX+200, y: 5 * self.frame.size.height / 8)
        speedLabel.zPosition = 100
        speedLabel.text = "\(String(answerResult))  游戏加速！！"
        self.addChild(speedLabel)
        let moveGroundSprite = SKAction.moveBy(x: -(UIScreen.main.bounds.maxX+400), y: 0, duration: TimeInterval(3))
        speedLabel.run(moveGroundSprite)
        Timer.scheduledTimer(withTimeInterval: 3, repeats: false) { (timer) in
            timer.invalidate()
            self.speedLabel.removeFromParent()
        }
    }
    
    //MARK:back button
    func setupBackBtn(){
        let btn = UIButton(frame: CGRect(x: 20, y: 64, width: 50, height: 50))
        btn.isUserInteractionEnabled = true
        btn.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(backBtnHandler(tap:))))
        btn.setImage(#imageLiteral(resourceName: "bu_video_close"), for: .normal)
        self.view?.addSubview(btn)
    }
    @objc func backBtnHandler(tap:UIGestureRecognizer){
        if tap.state == .ended{
            if sudu == 0{
                self.viewcontroller?.navigationController?.popViewController(animated: true)
            }else{
                self.sudu = 0
                let alert = UIAlertController(title: "退出将失去进度", message: "是否退出？", preferredStyle: .alert)
                alert.addAction(UIAlertAction(title: "继续", style: .default, handler: { (containute) in
                    self.sudu = 1
                }))
                alert.addAction(UIAlertAction(title: "退出", style: .cancel, handler: { (back) in
                    self.viewcontroller?.navigationController?.popViewController(animated: true)
                }))
                viewcontroller?.present(alert, animated: true, completion: nil)
                calTime = lastFrameTime
                    Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true) { (timer) in
                        self.calTime += 0.5
                        if self.sudu != 0{
                            timer.invalidate()
                        }
                    }
            }
        }
    }
    
    //MARK:添加精灵
    func setupCharacters(){
        //plant
        plantSprite = setupSprite(sourceName: "", width: 40, height: 40, positionX: UIScreen.main.bounds.width / 10, positionY: sideup, sender: self,UIntNum: 0x1)
        plantSprite.physicsBody?.isDynamic = false
        //insect
        insect = setupSprite(sourceName: "chong1", width: 50, height: 50, positionX: UIScreen.main.bounds.width + 100, positionY: sideup, sender: self,UIntNum: 0x2)
        //sun
        sun = setupSprite(sourceName: "sun1", width: 40, height: 40, positionX: UIScreen.main.bounds.width + 200, positionY: sideup, sender: self,UIntNum: 0x3)
    }
    
    //MARK:游戏帧率刷新
    var calTime : TimeInterval = 0
    override func update(_ currentTime: TimeInterval) {
        if lastFrameTime == 0{
            calTime = currentTime
            lastFrameTime = currentTime
        }
        
        //利用游戏计时器，游戏加速
        gametime = currentTime - calTime
        //10秒一次答题、加速
        if gametime > 10{
            if speed1 == false{
                speed1 = true
                showAlert(sender: viewcontroller!)
            }else{
                if speed2 == false{
                    speed2 = true
                    showAlert(sender: viewcontroller!)
                }else{
                    if speed3 == false{
                        speed3 = true
                        showAlert(sender: viewcontroller!)
                    }else{
                        if speed4 == false{
                            speed4 = true
                            showAlert(sender: viewcontroller!)
                        }
                    }
                }
            }
        }
        
        deltaTime = currentTime - lastFrameTime
        lastFrameTime = currentTime
        //
        backgroundMove(sprite1: background1, sprite2: background2, movingSpeed: 200*CGFloat(sudu))
        //insect move
        spriteMove(sprite: insect, speed: CGFloat(400*gamespeed*sudu),deltaTime: deltaTime)
        //sun move
        spriteMove(sprite: sun, speed: CGFloat(400*gamespeed*sudu), deltaTime: deltaTime)
    }
    
    
    //MARK:用户交互
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if sudu > 0{
            let plantPosition1 = CGPoint(x: UIScreen.main.bounds.width / 10, y: sideup)
            let plantPosition2 = CGPoint(x: UIScreen.main.bounds.width / 10, y: sidedown)
            if plantposition == 1{                       //在上面
                plantSprite.position = plantPosition2                    //去下面
                plantposition = 2
            }else{
                plantSprite.position = plantPosition1                    //去上面
                plantposition = 1
            }
        }else{
            reset()
        }
        
    }
    
    
    //MARK:游戏碰撞
    func didBegin(_ contact: SKPhysicsContact) {
        let bodyA = contact.bodyA
        let bodyB = contact.bodyB
        let c = bodyA.categoryBitMask + bodyB.categoryBitMask
        if c == 3{//植物 撞 虫
            AudioServicesPlayAlertSound(SystemSoundID(kSystemSoundID_Vibrate))
                if health > 1{
                    healthLost()
                }else{
                    healthArray[health-1].texture = SKTexture(image: heart2)
                    gameOver()
                }
        }else if c == 4{//植物 撞 太阳
            goal()
        }
    }
    //加分 重置 太阳
    func goal(){
        sun.removeFromParent()
        score += 1
        currentScore = score
        scoreLabel.text = String(score)
        scoreLabel.run(SKAction.sequence([SKAction.scale(to: 1.5, duration:TimeInterval(0.1)), SKAction.scale(to: 1.0, duration:TimeInterval(0.1))]))
        let random = Int.random(in: 0...1)
        var positiony = sideup
        if random == 0{
            positiony = sideup
        }else{
            positiony = sidedown
        }
        sun = setupSprite(sourceName: "sun1", width: 40, height: 40, positionX: UIScreen.main.bounds.width + 40 + CGFloat(Int.random(in: 0...1000)), positionY: positiony, sender: self,UIntNum: 0x3)
    }
    //游戏结束
    func gameOver(){
        self.isPaused = true
        assets += score
        UserDefaults.standard.setValue(assets, forKey: "assets")
        sudu = 0
        self.markScore()
        self.alpha = 0.5
        Alert().gameoverAlert(imageName: "gameover") { (gameover) in
            self.alpha = 1
        }
    }
    //MARK:重置游戏
    func reset(){
        self.removeAllChildren()
        self.isPaused = false
        //重置数据
        score = 0
        currentScore = score
        plantposition = 1
        lastFrameTime = 0
        gamespeed = 1
        health = characterArray[currentCharacter].Level
        sudu = 1
        //重置视图
        setupBackGround()
        setupLabel()
        setupCharacters()
        setupHealth(sender: self, health: health)
    }
    func markScore(){
        if let topscore = UserDefaults.standard.string(forKey: "topscore"){
            if score >= Int(topscore)!{
                UserDefaults.standard.set(score, forKey: "topscore")
            }
        }else{
            UserDefaults.standard.set(score, forKey: "topscore")
        }
    }
    //MARK:背景移动脚本
    func backgroundMove(sprite1:SKSpriteNode,sprite2:SKSpriteNode,movingSpeed:CGFloat)->Void{
        var pointX1 : CGFloat = background1.position.x
        var pointX2 : CGFloat = background2.position.x
        //建立两个点之间的联系
        if pointX1 < pointX2{
            pointX1 -= (movingSpeed * CGFloat(deltaTime))
            pointX2 = pointX1 + background1.size.width
            if pointX1 <=  -background1.size.width / 2{
                pointX1 = pointX2 + background1.size.width
            }
        }else{
            pointX2 -= movingSpeed * CGFloat(deltaTime)
            pointX1 = pointX2 + background1.size.width
            if pointX2 <= -background1.size.width / 2{
                pointX2 = pointX1 + background1.size.width
            }
        }
        sprite1.position.x = pointX1
        sprite2.position.x = pointX2
    }
    
    func exitButton(){
        let exitButton = UIButton(frame: CGRect(x: 20, y: 64, width: 77, height: 48))
        let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap(tap:)))
        exitButton.addGestureRecognizer(tap)
        self.view?.addSubview(exitButton)
    }
    
    @objc func handleTap(tap:UIGestureRecognizer){
        if tap.state == .ended{
        }
    }
    //MARK:加速提示
    func speedUp(){
        var countDownNum = 5
        //提示加速
        speedUpLabel()
        //计时3秒
        Timer.scheduledTimer(withTimeInterval: 1.0, repeats: true) { [self] timer in
            if countDownNum == 0 {
                  // 销毁计时器
                timer.invalidate()
            } else if countDownNum == 5{
                gamespeed += 0.1//1.1
                countDownNum -= 1
            } else if countDownNum == 4{
                gamespeed += 0.1//1.2
                countDownNum -= 1
            } else if countDownNum == 3{
                gamespeed += 0.1//1.3
                countDownNum -= 1
            } else if countDownNum == 2{
                gamespeed += 0.1//1.4
                countDownNum -= 1
            } else if countDownNum == 1{
                gamespeed += 0.1//1.5
                countDownNum -= 1
            }
        }
    }
    //MARK:减少生命值
    func healthLost(){
        //红心变黑
        healthArray[health-1].texture = SKTexture(image: heart2)
        //生命值-1
        health -= 1
        //怪物removefromsuperview
        insect.removeFromParent()
        //怪物重新载入
        insect = setupSprite(sourceName: "chong1", width: 50, height: 50, positionX: UIScreen.main.bounds.width + 100, positionY: sideup, sender: self,UIntNum: 0x2)
    }
    //MARK:弹窗模块
    func showAlert(sender:UIViewController){
        calTime = lastFrameTime
            Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true) { (timer) in
                self.calTime += 0.5
                if self.sudu != 0{
                    timer.invalidate()
                }
            }
        sudu = 0
        //建立alert
        alert = UIAlertController(title: "\(String(question))", message: "请选择正确答案", preferredStyle: .alert)
        //添加到alert里面
        for result in resultArray{
            alert.addAction(UIAlertAction(title: result, style: .default, handler: { [self] (default) in
                //选择选项后
                
                //判断答案控制流
                if result == currentResult{
                    answerResult = "回答正确+10"
                    var limTime = 10
                    Timer.scheduledTimer(withTimeInterval: 0.01, repeats: true) { (timer) in
                        if limTime == 0{
                            timer.invalidate()
                        }else{
                            limTime -= 1
                            score += 1
                            currentScore = score
                            scoreLabel.text = String(score)
                            scoreLabel.run(SKAction.sequence([SKAction.scale(to: 1.5, duration:TimeInterval(0.05)), SKAction.scale(to: 1.0, duration:TimeInterval(0.05))]))
                        }
                    }
                        
                }else{
                    answerResult = "回答错误"
                }
                sudu = 1
                speedUp()
                AQ().AQPrepare()
            }))
        }
        sender.present(alert, animated: true, completion: nil)
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
